//
//  ViewController.swift
//  CollectionView
//
//  Created by SuXinDe on 2019/2/21.
//  Copyright © 2019 su xinde. All rights reserved.
//

import UIKit

import SnapKit

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 100
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GridCell", for: indexPath)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        guard let unwrappedCell = cell as? GridCell else {
            return
        }
        unwrappedCell.label.text = "\(indexPath.row)"
        unwrappedCell.label.textColor = UIColor.red
        unwrappedCell.imageView.backgroundColor = (indexPath.row % 2 == 0) ? .orange : .blue
    }

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        
    }
    
    lazy var collectionView: UICollectionView = {
        let screenWidth = UIScreen.main.bounds.size.width
        let gridLayout = GridLayout(width: screenWidth)
        var view = UICollectionView(frame: .zero,
                                    collectionViewLayout: gridLayout)
        view.register(GridCell.classForCoder(),
                      forCellWithReuseIdentifier: "GridCell")
        view.dataSource = self
        view.delegate = self
        
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.top.bottom.left.right.equalToSuperview()
        }
        collectionView.backgroundColor = UIColor.white
        
    }


}

